package com.awcall.iot.service.impl;

import com.awcall.iot.common.ResponseResult;
import com.awcall.iot.model.VerifCode;
import com.awcall.iot.mapper.VerifCodeMapper;
import com.awcall.iot.service.IVerifCodeService;
import com.awcall.iot.utils.SendSMS;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 验证码表 服务实现类
 * </p>
 *
 * @author wuhui
 * @since 2018-03-22
 */
@Service
public class VerifCodeServiceImpl extends ServiceImpl<VerifCodeMapper, VerifCode> implements IVerifCodeService {

    @Override
    public ResponseResult getVerifCode(String telephone, int type) {
        int key = (int) ((Math.random() * 9 + 1) * 100000);
        VerifCode mobileCode = new VerifCode();
        mobileCode.setTelephone(telephone);
        EntityWrapper<VerifCode> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("telephone", telephone);
        entityWrapper.orderBy(VerifCode.ID, false);
        entityWrapper.last("LIMIT 1");
        mobileCode = this.selectOne(entityWrapper);
        String operateString = "";
        switch (type) {
            case 1:
                operateString = "修改密码";
                break;
            default:
                break;
        }
        if (mobileCode == null) {
            mobileCode = new VerifCode();
            mobileCode.setTelephone(telephone);
            mobileCode.setCreatedAt(new Date());
            mobileCode.setUpdatedAt(new Date());
            mobileCode.setCount(1);
            mobileCode.setType(type);
            mobileCode.setCode(key + "");
            if (this.insert(mobileCode)) {
                return mobileCodeTemp(mobileCode, operateString);
            }
        } else {
            mobileCode.setCount(mobileCode.getCount() + 1);
            mobileCode.setCode(key + "");
            mobileCode.setType(type);
            mobileCode.setUpdatedAt(new Date());
            if (this.updateById(mobileCode)) {
                return mobileCodeTemp(mobileCode, operateString);
            }

        }
        return new ResponseResult(1, "验证码获取异常");
    }

    @Override
    public ResponseResult verfiCode(String telephone, int type, String code) {
        VerifCode verifCode = new VerifCode();
        EntityWrapper<VerifCode> ewVerif = new EntityWrapper<>();
        ewVerif.eq("telephone", telephone);
        ewVerif.eq("type", type);
        ewVerif.orderBy(VerifCode.ID, false);
        ewVerif.last("LIMIT 1");
        verifCode = verifCode.selectOne(ewVerif);
        if (null == verifCode || !code.equals(verifCode.getCode())) {
            return new ResponseResult<>(1, "验证码输入错误，请重新输入或关闭重新获取");
        }
        Long dateTime = verifCode.getUpdatedAt().getTime();

        long currentTimeMillis = System.currentTimeMillis();
        long temp = 10 * 60 * 1000;

        if ((currentTimeMillis - dateTime) > temp) {
            return new ResponseResult<>(1, "验证码已经失效，请重新输入或关闭重新获取！");
        }

        verifCode.deleteById();
        return new ResponseResult(0, "验证成功！");
    }

    private ResponseResult mobileCodeTemp(VerifCode mo, String operateMsg) {
        String msgPre = "【大牛飞猫】尊敬的用户，(" + operateMsg + ")手机验证码为：";
        String msgEnd = "。该验证码10分钟内有效，如非本人操作请忽略。";

        //发送验证码逻辑
        String msg = msgPre + mo.getCode() + msgEnd;
        SendSMS.send(mo.getTelephone(), msg, "send");
        return new ResponseResult(0, "发送成功注意查收");
    }
}
